//
//  PositioningClass.m
//  testpbl
//
//  Created by 黄权浩 on 15/8/24.
//  Copyright (c) 2015年 黄权浩. All rights reserved.
//

#import "PositioningClass.h"

@implementation PositioningClass

- (id)init
{
    self=[super init];
    if(self){
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy=kCLLocationAccuracyBest;
        _locationManager.distanceFilter=10;
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >=8 ) {
            [_locationManager requestAlwaysAuthorization];
        }
        [_locationManager startUpdatingLocation];
    }
    return self;
}

+ (instancetype)shared
{
    static PositioningClass *class = nil;
    static dispatch_once_t once;
    dispatch_once(&once,^{
        class = [[PositioningClass alloc] init];
    });
    return class;
}

#pragma mark - Delegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    //locations数组里边存放的是CLLocation对象，一个CLLocation对象就代表着一个位置
    CLLocation *loc = [locations firstObject];
    //维度：loc.coordinate.latitude //经度：loc.coordinate.longitude
    NSLog(@"纬度=%f，经度=%f",loc.coordinate.latitude,loc.coordinate.longitude);
    NSLog(@"%lu",(unsigned long)locations.count);
    // 获取当前所在的城市名
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    //根据经纬度反向地理编译出地址信息
    [geocoder reverseGeocodeLocation:loc completionHandler:^(NSArray *array, NSError *error)
     {
         if (array.count > 0)
         {
             CLPlacemark *placemark = [array objectAtIndex:0];
             
             //将获得的所有信息显示到label上
             //获取城市
             NSString *city = placemark.locality;
             if (!city) {
                 city = placemark.administrativeArea;
             }
             NSLog(@"city = %@", city);
             NSLog(@"name:%@\n country:%@\n postalCode:%@\n ISOcountryCode:%@\n ocean:%@\n inlandWater:%@\n locality:%@\n subLocality:%@ \n administrativeArea:%@\n subAdministrativeArea:%@\n thoroughfare:%@\n subThoroughfare:%@\n",
                   placemark.name,
                   placemark.country,
                   placemark.postalCode,
                   placemark.ISOcountryCode,
                   placemark.ocean,
                   placemark.inlandWater,
                   placemark.administrativeArea,
                   placemark.subAdministrativeArea,
                   placemark.locality,
                   placemark.subLocality,
                   placemark.thoroughfare,
                   placemark.subThoroughfare);
             
         }
         else if (error == nil && [array count] == 0)
         {
             NSLog(@"No results were returned.");
         }
         else if (error != nil)
         {
             NSLog(@"An error occurred = %@", error);
         }
     }];
    //发送定位消息
    //若只需要定位一次服务则需要取消定位
    [self.locationManager stopUpdatingLocation];
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    if ([error code]==kCLErrorDenied) {
        NSLog(@"访问被拒绝");
        //用户没有给予用户使用定位的权限
        UIAlertView *locationAlert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Please open the location services" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
        [locationAlert show];
    }
    if ([error code]==kCLErrorLocationUnknown) {
        NSLog(@"无法获取位置信息");
        //wife 数据  基站服务 GPS瘫痪
    }
}


@end
